import { getArticleList, getArticleTag,getCategoryList } from '@/services';
import { pageData } from "../../utils/modules/apiDatas"
const state = {
  articleList: [],
  articleTag: [],
  categoryList:[],
  page: 0,
  totalNum: 0,
  pageData: pageData,
};

const mutations = {
  update(state, payload) {
    for (let key in payload) {
      // console.log(key);
      state[key] = payload[key];
    }
  },
};

const actions = {
  async getArticleList({ commit, state }) {
    let result = await getArticleList();
    // console.log(result);
    if (result) {
      let articleList = result[0];
      if (state.page > 1) {
        articleList = [...state.articleList, ...articleList];
      }
      commit("update", {
        articleList,
        totalNum: result[1],
      });
    }
  },
  async getArticleTag({ commit }) {
    let result = await getArticleTag();
    if (result) {
      let articleTag = result;
      commit("update", {
        articleTag,
      });
    }
  },
  async getCategoryList({ commit }) {
    let result = await getCategoryList();
    if (result) {
      let categoryList = result;
      commit("update", {
        categoryList,
      });
    }
  },
  async getCategoryData({ state },id){
    let articleList =[];state.articleList.map(item=>{
      if(item.category){
        if(item.category.label===id){
          articleList.push(item)
        }
      }
      return articleList
    })
    // console.log(articleList);
      // commit("update", {
      //   articleList,
      // });
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
